package com.doangeometrydash.manager;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.Scene;
import com.doangeometrydash.base.BaseScene;
import com.doangeometrydash.scene.AboutScene;
import com.doangeometrydash.scene.GameOverScene;
import com.doangeometrydash.scene.GameScene;
import com.doangeometrydash.scene.LoadingScene;
import com.doangeometrydash.scene.MainMenuScene;
import com.doangeometrydash.scene.OptionsMenuScene;
import com.doangeometrydash.scene.SplashScene;

public class SceneManager
{
	private BaseScene splashScene;
	private BaseScene menuScene;
	private BaseScene gameScene;
	private BaseScene loadingScene;
	private BaseScene gameOverScene;
	private BaseScene optionsMenuScene;
	private BaseScene aboutScene;

	private int mapLevel = 1;

	private static final SceneManager INSTANCE = new SceneManager();

	private SceneType currentSceneType = SceneType.SCENE_SPLASH;

	private BaseScene currentScene;

	private Engine mEngine = ResourcesManager.getInstance().mEngine;

	public enum SceneType
	{
		SCENE_SPLASH, SCENE_MENU, SCENE_GAME, SCENE_LOADING, SCENE_OPTION, SCENE_GAMEOVER, SCENE_ABOUT
	}

	public void setScene(BaseScene scene)
	{
		mEngine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}

	public void setScene(SceneType sceneType)
	{
		switch (sceneType)
		{
		case SCENE_MENU:
			setScene(menuScene);
			break;
		case SCENE_GAME:
			setScene(gameScene);
			break;
		case SCENE_SPLASH:
			setScene(splashScene);
			break;
		case SCENE_LOADING:
			setScene(loadingScene);
			break;
		case SCENE_OPTION:
			setScene(optionsMenuScene);
			break;
		case SCENE_GAMEOVER:
			setScene(gameOverScene);
			break;
		case SCENE_ABOUT:
			setScene(aboutScene);
			break;
		default:
			break;
		}
	}

	public Scene createAboutScene()
	{
		ResourcesManager.getInstance().loadAboutResources();
		aboutScene = new AboutScene();
		setScene(aboutScene);
		return currentScene;
	}

	public Scene createOptionsMenuScene()
	{
		ResourcesManager.getInstance().loadOptionResources();
		if (optionsMenuScene == null)
			optionsMenuScene = new OptionsMenuScene();
		setScene(optionsMenuScene);
		return currentScene;
	}

	public void loadGameScene()
	{
		setScene(loadingScene);
		mEngine.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback()
		{
			public void onTimePassed(final TimerHandler pTimerHandler)
			{
				mEngine.unregisterUpdateHandler(pTimerHandler);
				ResourcesManager.getInstance().loadGameResources(mapLevel);
				gameScene = new GameScene();
				setScene(gameScene);
			}
		}));
	}

	public void loadGameSceneAgain()
	{
		setScene(loadingScene);
		mEngine.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback()
		{
			public void onTimePassed(final TimerHandler pTimerHandler)
			{
				mEngine.unregisterUpdateHandler(pTimerHandler);
				gameScene = new GameScene();
				setScene(gameScene);
			}
		}));
	}

	public void loadNextGameScene()
	{
		if (mapLevel <= 2)
			mapLevel++;
		loadGameScene();
	}

	public Scene createGameOverScene()
	{
		ResourcesManager.getInstance().loadGameOverResources();
		gameOverScene = new GameOverScene();
		setScene(gameOverScene);
		return currentScene;
	}

	public Scene createMenuScene()
	{
		ResourcesManager.getInstance().loadMenuResources();
		menuScene = new MainMenuScene();
		loadingScene = new LoadingScene();
		loadMenuScene();
		if (ResourcesManager.getInstance().muteOption)
			mEngine.getMusicManager().setMasterVolume(0.0f);
		disposeSplashScene();
		return currentScene;
	}

	public void loadMenuScene()
	{
		setScene(menuScene);
		ResourcesManager.getInstance().soundTrackMenu.play();
	}

	public Scene createSplashScene()
	{
		ResourcesManager.getInstance().loadSplashScreen();
		splashScene = new SplashScene();
		currentScene = splashScene;
		return currentScene;
	}

	private void disposeSplashScene()
	{
		ResourcesManager.getInstance().unloadSplashScreen();
		splashScene.disposeScene();
		splashScene = null;
	}

	public static SceneManager getInstance()
	{
		return INSTANCE;
	}

	public SceneType getCurrentSceneType()
	{
		return currentSceneType;
	}

	public BaseScene getCurrentScene()
	{
		return currentScene;
	}
}
